<?php

namespace App\Http\Controllers\Home;

use App\Model\Adress;
use App\Model\Goodlist;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\View;
use App\lib\Cart;
use App\Model\Order;
use App\Model\Orderlist;
use App\Model\Goods;


class OrderController extends CommonController
{
    //订单控制器

    //确认订单
    public function order(Cart $cart, Request $request)
    {
        //获取购物车
        $goods = $cart->getAllData();
        if ($request->method() == 'POST') {
            //添加订单数据
            $order = new Order;
            $order->no = $cart->getOrderId();
            $order->allprice = $goods['total'];
            $order->uid = session('user.id');
            $order->addrid = $request->addrid;
            $order->paytype = $request->paytype;
            $order->orderstatus = 1;
            $order->save();
            $oid = $order->id;
            //添加订单详情数据
            foreach ($goods['goods'] as $good) {
                $orderlist = new Orderlist;
                $orderlist->oid = $oid;
                $orderlist->price = $good['price'];
                $orderlist->total = $good['total'];
                $orderlist->num = $good['num'];
                $orderlist->title = $good['options']['title'];
                $orderlist->img = $good['options']['imgs'];
                $orderlist->lists = $good['options']['lists'];
                $orderlist->desc = json_encode(Goods::find($good['id']));
                $orderlist->save();
            }

            return redirect('/ordersuccess/' . $oid);
        };
        //收货地址
        $adress = Adress::where('uid', session('user.id'))->get();
        return view('home.order.order', compact('goods', 'adress'));
    }

    //订单成功
    public function orderSuccess($oid = '', Cart $cart)
    {
        //将订单中的产品数量更新
        $allCartDatas = $cart->getGoods();
        foreach ($allCartDatas as $allCartData) {
            $goodMap = [
                ['goods_id', $allCartData['id']],
                ['lists', $allCartData['options']['lists']]
            ];
            $goodsdata = Goodlist::where($goodMap)->decrement('stock', $allCartData['num']);
        }
        //删除购物车
        $cart->flush();
        $orderData = Order::find($oid);


        //收货信息
        $adress = Adress::find($orderData['addrid']);
        return view('home.order.ordersuccess', compact('orderData','adress'));
    }

    //订单列表
    public function orderlist()
    {
        $orderlists = Order::where('uid', session('user.id'))->orderBy('id', 'desc')->paginate(5);;
        return view('home.order.orderlist', compact('orderlists'));
    }

    //更新订单状态
    public function changeorder($id, $status)
    {
        $order = Order::find($id);
        $order->orderstatus = $status;
        $order->save();
        return redirect('/orderlist/')->with('status', '操作成功');
    }

    //模拟付款
    public function pay($id)
    {
        //订单状态1新订单 2付款 3发货 4完成 5退货
        $order = Order::find($id);
        $order->orderstatus = 2;
        $order->save();
        return redirect('/orderlist');
    }
}
